package com.example.dell.intelligentagriculture;




import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.example.dell.intelligentagriculture.Service.WebService;
import com.example.dell.intelligentagriculture.fragment.Co2Fragment;
import com.example.dell.intelligentagriculture.fragment.GuangzhaoFragment;
import com.example.dell.intelligentagriculture.fragment.KongqiFragment;
import com.example.dell.intelligentagriculture.fragment.TuranFragment;
import com.example.dell.intelligentagriculture.utils.Agriculture;
import com.example.dell.intelligentagriculture.utils.HttpUtil;
import com.example.dell.intelligentagriculture.utils.MyApp;

import org.json.JSONException;

import java.io.IOException;

public class ControlActivity extends AppCompatActivity implements View.OnClickListener {
    private FragmentTransaction transaction;
    private TextView title;
    private ImageView dakaifengshan, dakaishui, dakaiguang, dakaibaojing;
    private HttpUtil util;
    private MyApp myApp;
    private int[] status={0,0,0,0};//0 0 0 0
    private MyReceiver myReceiver;
    public Agriculture agriculture;
    private Co2Fragment co2Fragment;
    private GuangzhaoFragment guangzhaoFragment;
    private TuranFragment turangFragment;
    private KongqiFragment kongqiFragment;
    private int item;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            int states = msg.arg1;
            switch (msg.what) {
                case 999:
                    status = (int[]) msg.obj;
                    if (status[0] == 1) {
                        dakaishui.setImageResource(R.mipmap.dakaishui2);
                    } else {
                        dakaishui.setImageResource(R.mipmap.dakaishui);
                    }
                    if (status[1] == 1) {
                        dakaifengshan.setImageResource(R.mipmap.dakaifengshan2);
                    } else {
                        dakaifengshan.setImageResource(R.mipmap.dakaifengshan);
                    }
                    if (status[2] == 1) {
                        dakaiguang.setImageResource(R.mipmap.dakaiguangzhao2);
                    } else {
                        dakaiguang.setImageResource(R.mipmap.dakaiguangzhao);
                    }
                    if (status[3] == 1) {
                        dakaibaojing.setImageResource(R.mipmap.dakaibaojing2);
                    } else {
                        dakaibaojing.setImageResource(R.mipmap.dakaibaojing);
                    }
                    break;
                case 100:
                    if (states == 1) {
                        dakaibaojing.setImageResource(R.mipmap.dakaibaojing2);
                    } else {
                        dakaibaojing.setImageResource(R.mipmap.dakaibaojing);
                    }
                    break;
                case 200:
                    if (states == 1) {
                        dakaifengshan.setImageResource(R.mipmap.dakaifengshan2);
                    } else {
                        dakaifengshan.setImageResource(R.mipmap.dakaifengshan);
                    }
                    break;
                case 300:
                    if (states == 1) {
                        dakaiguang.setImageResource(R.mipmap.dakaiguangzhao2);
                    } else {
                        dakaiguang.setImageResource(R.mipmap.dakaiguangzhao);
                    }
                    break;
                case 400:
                    if (states == 1) {
                        dakaishui.setImageResource(R.mipmap.dakaishui2);
                    } else {
                        dakaishui.setImageResource(R.mipmap.dakaishui);
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_control);
        util = new HttpUtil();
        myApp = (MyApp) getApplication();
       /* Intent intent = getIntent();
        status = intent.getIntArrayExtra("status");*/
        getStatus();
        initView();
        initReceiver();
        loadFragment();
    }


    /**
     * 初始化控件
     */
    private void initView() {
        // getStatus();
        FragmentManager fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        title = findViewById(R.id.title);
        dakaifengshan = findViewById(R.id.dakaifengshan);
        dakaishui = findViewById(R.id.dakaishui);
        dakaiguang = findViewById(R.id.dakaiguangzhao);
        dakaibaojing = findViewById(R.id.dakaibaojing);

        dakaishui.setOnClickListener(this);
        dakaifengshan.setOnClickListener(this);
        dakaiguang.setOnClickListener(this);
        dakaibaojing.setOnClickListener(this);
    }

    /**
     * 动态加载fragment
     */
    private void loadFragment() {
        Intent intent = getIntent();
        item = intent.getIntExtra("item", 1);
        switch (item) {
            case 1:
                //co2
                title.setText(getResources().getString(R.string.item1_a));
                co2Fragment = new Co2Fragment();
                transaction.replace(R.id.framelayout, co2Fragment).commit();
                dakaishui.setVisibility(View.GONE);
                dakaiguang.setVisibility(View.GONE);
                break;
            case 2:
                title.setText(getResources().getString(R.string.item2_a));
                guangzhaoFragment = new GuangzhaoFragment();
                transaction.replace(R.id.framelayout, guangzhaoFragment).commit();
                dakaishui.setVisibility(View.GONE);
                dakaifengshan.setVisibility(View.GONE);
                break;
            case 3:
                title.setText("土壤详情");
                turangFragment = new TuranFragment();
                transaction.replace(R.id.framelayout, turangFragment).commit();
                dakaifengshan.setVisibility(View.GONE);
                break;
            case 4:
                title.setText("空气详情");
                kongqiFragment = new KongqiFragment();
                transaction.replace(R.id.framelayout, kongqiFragment).commit();

                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dakaibaojing:
                if (status[3] == 0) {
                    contorlShapan("Buzzer", 1, 100);
                    status[3] = 1;
                } else {
                    contorlShapan("Buzzer", 0, 100);
                    status[3] = 0;
                }
                break;
            case R.id.dakaifengshan:
                if (status[1] == 0) {
                    contorlShapan("Blower", 1, 200);
                    status[1] = 1;
                } else {
                    contorlShapan("Blower", 0, 200);
                    status[1] = 0;
                }

                break;
            case R.id.dakaiguangzhao:
                if (status[2] == 0) {
                    contorlShapan("Roadlamp", 1, 300);
                    status[2] = 1;
                } else {
                    contorlShapan("Roadlamp", 0, 300);
                    status[2] = 0;
                }

                break;
            case R.id.dakaishui:
                if (status[0] == 0) {
                    contorlShapan("WaterPump", 1, 400);
                    status[0] = 1;
                } else {
                    contorlShapan("WaterPump", 0, 400);
                    status[0] = 0;
                }

                break;
        }
    }

    /**
     * 获取控制器状态
     */

    private void getStatus() {
        new Thread() {
            @Override
            public void run() {
                try {
                    int[] statusArr = util.getContorllerStatus(myApp);
                    Message message = new Message();
                    message.what = 999;
                    message.obj = statusArr;
                    handler.sendMessage(message);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    /**
     * 操控沙盘
     *
     * @param controlName
     * @param value
     */
    private void contorlShapan(final String controlName, final int value, final int flag) {
        new Thread() {
            @Override
            public void run() {
                try {
                    util.control(myApp, controlName, value);
                    Message message = new Message();
                    message.what = flag;
                    message.arg1 = value;
                    handler.sendMessage(message);
                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }


    /**
     * 注册广播接收者
     */
    private void initReceiver() {
        myReceiver = new MyReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(WebService.ACTION);
        registerReceiver(myReceiver, intentFilter);
    }

    /*
     * 广播接收者
     * */
    private class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (WebService.ACTION.equals(action)) {
                agriculture = (Agriculture) intent.getSerializableExtra("mySensor");
                switch (item){
                    case 1:
                        co2Fragment.getAgrculture(agriculture);
                        break;
                    case 2:
                        guangzhaoFragment.getAgrculture(agriculture);
                        break;
                    case 3:
                      //  turangFragment.getAgriculture(agriculture);
                        break;
                    case 4:
                      //  kongqiFragment.getAgriculture(agriculture);
                        break;
                }


            }
        }
    }


}





